#ifndef webhttpd__HTTPREQUEST
#define webhttpd__HTTPREQUEST

#include <glib.h>
#include <uv.h>
#include <srclink/utils.h>


typedef struct HttpRequestBody {
    char *content;
    size_t size;
} HttpRequestBody;

typedef struct HttpRequest {
    GHashTable *headers;
    char method[10];
    char path[10240];
    char protocol[20];
    char remote_addr[INET_ADDRSTRLEN];
    int remote_port;
    HttpRequestBody body;
} HttpRequest;

#define INVALID_HTTPREQUEST NULL

/**
 * @brief 解析 HTTP 请求
 *
 * @param raw_content 原始内容
 * @param size 内容大小
 * @return 解析后的 HTTP 请求结构体
 */
static inline struct HttpRequest *parse_request(char *raw_content, size_t size) {
    // 创建请求对象
    struct HttpRequest *request = (struct HttpRequest *) malloc(sizeof(struct HttpRequest));
    if (request == NULL) {
        return INVALID_HTTPREQUEST;
    }

    // 初始化结构体
    request->headers = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
    request->body.content = NULL;
    request->body.size = 0;

    int index = 0;
    int first_line = 1;
    int line_index = 0;

    unsigned char line[BUFFER_SIZE];
    memset(line, 0, BUFFER_SIZE);

    for (index = 0; index < size - 1; index++) {
        if (raw_content[index] == '\r' && raw_content[index + 1] == '\n') {
            index++; // 换行符pass

            if (line_index == 0) {
                // 空行，说明请求头结束，下面是正文
                break;
            }

            int count = 0;
            char *token = strtok(line, " ");

            char header_key[BUFFER_SIZE];
            char header_value[BUFFER_SIZE];

            while (token != NULL) {
                if (first_line) {
                    // 第一行：请求方法、路径、协议
                    switch (count++) {
                        case 0:
                            strncpy(request->method, token, sizeof(request->method) - 1);
                            break;
                        case 1:
                            strncpy(request->path, token, sizeof(request->path) - 1);
                        // 检查路径安全性
                            if (strstr(request->path, "../") != NULL || strstr(request->path, "..\\") != NULL) {
                                free(request);
                                return INVALID_HTTPREQUEST;
                            }
                            break;
                        case 2:
                            strncpy(request->protocol, token, sizeof(request->protocol) - 1);
                            break;
                        default:
                            break;
                    }
                } else {
                    // 请求头解析
                    token++; // 跳过换行符
                    switch (count++) {
                        case 0:
                            // 计算 header_key 的长度
                            char *found_pos = strstr(token, ":");
                            if (found_pos == NULL) {
                                // 不正确的
                                return NULL;
                            }
                            size_t length = found_pos - token;
                            strncpy(header_key, token, length);
                            header_key[length] = '\0'; // 确保字符串结束
                            break;
                        case 1:
                            // 获取 header_value
                            strncpy(header_value, token, sizeof(header_value) - 1);
                            header_value[sizeof(header_value) - 1] = '\0'; // 确保字符串结束
                            break;
                        default:
                            // 拼接剩余内容，header_value 可能有多行，继续处理
                            if (strchr(token, '\n') != NULL) {
                                // 如果 token 中含有换行符，则将换行符之后的内容拼接
                                strncat(header_value, token, sizeof(header_value) - strlen(header_value) - 1);
                            } else {
                                // 如果没有换行符，直接将剩余部分拼接
                                strncat(header_value, " ", sizeof(header_value) - strlen(header_value) - 1);
                                strncat(header_value, token, sizeof(header_value) - strlen(header_value) - 1);
                            }
                            break;
                    }
                }

                token = strtok(NULL, " ");
            }

            // 处理第一行结束后的协议检查
            if (first_line) {
                first_line = 0;
                if (strcmp(request->protocol, "HTTP/1.1") != 0) {
                    free(request);
                    return INVALID_HTTPREQUEST;
                }
            } else {
                // 插入请求头到哈希表
                g_hash_table_insert(request->headers, strdup(header_key), strdup(header_value));
            }

            // 清除缓存行数据
            memset(line, 0, BUFFER_SIZE);
            line_index = 0;
        } else {
            line[line_index++] = raw_content[index];
            if (line_index >= BUFFER_SIZE) {
                // 防止超出缓存
                free(request);
                return INVALID_HTTPREQUEST;
            }
        }
    }

    // 请求正文部分

    if (index >= size) {  // 没有正文
        request->body.size = 0;
        request->body.content = NULL;
    } else {
        index += 2; // 跳过 '\r\n' 结束
        request->body.size = size - index + 1;
        request->body.content = malloc(request->body.size + 1);

        if (request->body.size != 0) {
            strncpy(request->body.content, raw_content + index, request->body.size);
            request->body.content[request->body.size] = '\0'; // 确保正文字符串结束
        } else {
            request->body.content[0] = '\0';
        }
    }

    return request;
}

#endif
